import {
  addOrderData,
  getOrderData,
  getMyOrderData,
  cancelOrderData,
  sureOrderData,
  orderDetailsData,
  getReviewOrderData,
  getReviewServiceData,
  addReviewsData
} from 'api/order'

export default {
  namespaced: true,
  state: {
    orderNum: '',
    orders: [],
    orderDetails: {},
    reviewOrders: [],
    reviewServices: []
  },
  mutations: {
    ['orderNum'] (state, payload) {
      state.orderNum = payload.orderNum
    },
    ['SET_ORDERS'] (state, payload) {
      state.orders = payload.orders
    },
    ['SET_ORDERS_PAGE'] (state, payload) {
      state.orders.push(...payload.orders)
    },
    ['CANCEL_ORDER'] (state, payload) {
      let delIndex = ''
      state.orders.forEach((item, index) => {
        if (item.ordernum === payload.ordernum) {
          delIndex = index
        }
      })
      state.orders.splice(delIndex, 1)
    },
    ['SURE_ORDER'] (state, payload) {
      state.orders.forEach((item, index) => {
        if (item.ordernum === payload.ordernum) {
          state.orders[index].status = 2
        }
      })
    },
    ['ORDER_DETAILS'] (state, payload) {
      state.orderDetails = payload.orderDetails
    },
    ['SET_REVIEW_ORDERS'] (state, payload) {
      if (payload.page === 1) {
        state.reviewOrders = payload.reviewOrders
      } else {
        state.reviewOrders.push(...payload.reviewOrders)
      }
    },
    ['SET_REVIEW_ERVICES'] (state, payload) {
      let reviewServices = []
      // 组装评价表单需要的数据和前台页面方便打分的便捷数据
      if (payload.reviewServices) {
        let tmp = payload.reviewServices
        tmp.forEach((item) => {
          item.score = 5
          item.starts = [
            {
              value: 1,
              active: true
            },
            {
              value: 2,
              active: true
            },
            {
              value: 3,
              active: true
            },
            {
              value: 4,
              active: true
            },
            {
              value: 5,
              active: true
            },
          ]
          reviewServices.push(item)
        })
      }
      state.reviewServices = reviewServices
      // console.log(reviewServices)
    },
    ['SET_SCORE'] (state, payload) {
      if (state.reviewServices) {
        state.reviewServices[payload.index].starts.forEach((item, index) => {
          if (index <= payload.index2) {
            state.reviewServices[payload.index].starts[index].active = true
          } else {
            state.reviewServices[payload.index].starts[index].active = false
          }
        })
        state.reviewServices[payload.index].score = state.reviewServices[payload.index].starts[payload.index2].value
      }
    }
  },
  actions: {
    // 提交订单
    addOrder (context, payload) {
      return addOrderData({
        uid: context.rootState.user.uid,
        ...payload
      }).then(res => res)
    },
    // 获取最后提交的订单编号
    getOrderNum (context) {
      getOrderData(context.rootState.user.uid).then(res => {
        if (res.code === 200) {
          context.commit('orderNum', { orderNum: res.data.ordernum })
        }
      })
    },

    //我的订单
    getMyOrder (context, payload) {
      return getMyOrderData({
        uid: context.rootState.user.uid,
        status: payload.status,
        page: payload.page
      }).then(res => {
        if (payload.page === 1) {
          if (res.code === 200) {
            context.commit('SET_ORDERS', { orders: res.data })
          } else {
            context.commit('SET_ORDERS', { orders: [] })
          }
        } else {
          if (res.code === 200) {
            context.commit('SET_ORDERS_PAGE', { orders: res.data })
          }
        }
        return res
      })
    },

    // 取消订单
    cancelOrder (context, payload) {
      return cancelOrderData({
        uid: context.rootState.user.uid,
        ordernum: payload.ordernum
      }).then(res => {
        context.commit('CANCEL_ORDER', { index: payload.index, ordernum: payload.ordernum })
        return res
      })
    },

    // 确认收货
    sureOrder (context, payload) {
      return sureOrderData({
        uid: context.rootState.user.uid,
        ordernum: payload.ordernum
      }).then(res => {
        context.commit('SURE_ORDER', { index: payload.index, ordernum: payload.ordernum })
        return res
      })
    },

    // 会员订单详情
    getOrderDetails (context, payload) {
      return orderDetailsData({
        uid: context.rootState.user.uid,
        ordernum: payload.ordernum
      }).then(res => {
        if (res.code === 200) {
          context.commit('ORDER_DETAILS', { orderDetails: res.data })
        }
        return res
      })
    },

    // 待评价订单
    getReviewOrder (context, payload) {
      let page = payload.page ? payload.page : 1
      return getReviewOrderData({
        uid: context.rootState.user.uid,
        page: page
      }).then(res => {
        if (res.code === 200) {
          context.commit('SET_REVIEW_ORDERS', { reviewOrders: res.data, page: page })
        } else {
          context.commit('SET_REVIEW_ORDERS', { reviewOrders: [], page: page })
        }
        return res
      })
    },

    // 获取评价选项
    getReviewServices (context) {
      getReviewServiceData().then(res => {
        if (res.code === 200) {
          context.commit('SET_REVIEW_ERVICES', { reviewServices: res.data })
        }
      })
    },
    // 添加评价
    addReviews (context, payload) {
      return addReviewsData({
        ...payload
      })
        .then(res => res)
    }
  }
}
